// pages/memeberOrder/memeberOrder.js
var page = 1;
var app = getApp()
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var jlpay1 = app.data.jlway;
const commonURLXX = app.data.XCX_PAY;
var commonURLWW = app.data.testURL;
Page({
  data: {
    "navTab": ["全部", "待发货", "待收货", "已收货"],
    'status': '',
    "currentNavtab": "0",
    onclose_cover: false,
    array: ['手机号', '昵称', '姓名'],
    condition: '',
    cvalue: '',
    selectindex:''
  },
  onLoad: function(options) {
    this.getData('', '', '', '', 5)
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = Number(e.detail.value) + 1
    this.setData({
      index: e.detail.value,
      selectindex: index,
      condition: this.data.array[e.detail.value]
    })
  },
  searchchange: function(e) {
    this.setData({
      cvalue: e.detail.value
    })
  },

  switchTab: function(e) {
    var that = this;
    page = 1;
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
    var item = e.currentTarget.dataset.idx;
    console.log(item)
    var status = 0;
    switch (that.data.currentNavtab) {
      case 0:
        status = "";
        break;
      case 1:
        status = "8";
        break;
      case 2:
        status = "2";
        break;
      case 3:
        status = "3";
        break;
    }
    that.setData({
      status: status
    })
    console.log(status)
    this.getData(status, '', '', '', 5)
  },
  search: function() {
    this.getData(this.data.status, this.data.selectindex, this.data.cvalue, 1, 5)
  },
  getData: function(status, condition, cvalue, page, pagesize) {
    var that = this;
    wx.request({
      url: jlpay1 + 'op=getGroupOrders',
      data: {
        veropenid: wx.getStorageSync('openid'),
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        statu: status,
        condition: condition,
        cvalue: cvalue,
        page: page,
        pagesize: pagesize
      },
      success: function(res) {
        console.log(res.data.data)
        that.setData({
          data: res.data.data,
          goodsnum: res.data.goodsnum,
          ordernum: res.data.ordernum
        })
      }
    })
  },
  onReachBottom: function() {
    var that = this;
    var status = 0;
    switch (that.data.currentNavtab) {
      case 0:
        status = "";
        break;
      case 1:
        status = "8";
        break;
      case 2:
        status = "2";
        break;
      case 3:
        status = "3";
        break;
    }

    page++;
    // this.getData(Number(this.data.id), status, '', '', 0, 10)
    wx.request({
      url: jlpay1 + 'op=getGroupOrders',
      data: {
        veropenid: wx.getStorageSync('openid'),
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        statu: this.data.status,
        condition: this.data.selectindex,
        cvalue: this.data.cvalue,
        page: page,
        pagesize: 5
      },
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log(res.data)
        var data = res.data.data;
        if (data.length < 1) {
          wx.showToast({
            title: '全部订单加载完成',
            icon: 'success',
            duration: 1000
          })
          return false;
        }
        wx.showToast({
          title: '订单加载中...',
          icon: 'loading',
          duration: 1000
        })

        // 我们用total和count来控制分页，total代表已请求数据的总数，count代表每次请求的个数。
        // 上拉时需把total在原来的基础上加上count，代表从count条后的数据开始请求。
        var goodsnum = that.data.goodsnum + res.data.goodsnum
        var ordernum = that.data.ordernum + res.data.ordernum
        that.setData({
          data: that.data.data.concat(res.data.data),
          goodsnum: goodsnum,
          ordernum:ordernum
        });
      }
    })
  },
  zhuangtai: function(status) {
    switch (status) {
      case "0": //等待 付款******  
        return "待付款";
        break;
      case "1": //支付成功  
        return "已支付";
        break;
      case "8": //等待商家发货    0 1 8
        return "待发货";
        break;
      case "2": //订单已发货  0 1 8 2
        return "待收货";
        break;
      case "3": //订单完成  0 1 8 2 3
        return "已收货";
        break;
      case "10": //等待退款 0 1 10
        return "待退款";
        break;
      case "4": //订单已退款  0 1 10 4
        return "已退款";
        break;
      case "7": //订单已退款  0 1 10 7
        return "已退款";
        break;
      case "6": //部分退款  0 1 8 2 6
        return "部分退款";
        break;
      case "9": //订单已取消  0 9****
        return "已取消";
        break;
      case "5": //订单已取消  0 9****
        return "部分签收";
        break;
    }
  },
})